#include <bits/stdc++.h>
using namespace std;
const int N = 4e3 + 10, M = 1e5 + 10;
int n, m, cnt;
typedef long long ll;
ll dp[M], ans;
struct P {
  int c, f, v;
  bool operator<(const P &a) const {
    // 时钟频率相同时，价格低的的优先；
    // 买入的价值为负，故买入在前
    if (f == a.f) return v < a.v;
    // 时钟频率大的优先
    return f > a.f;
  }
} a[N];
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].c >> a[i].f >> a[i].v;
    a[i].v = -a[i].v;
  }
  cin >> m;
  for (int i = n + 1; i <= n + m; ++i) {
    cin >> a[i].c >> a[i].f >> a[i].v;
  }
  sort(a + 1, a + n + m + 1);
  memset(dp, -0x3f, sizeof dp);  // 初始化为-INF
  dp[0] = 0;
  for (int i = 1; i <= n + m; ++i) {
    auto [c, f, v] = a[i];
    if (v < 0) {  // 买入
      for (int j = cnt; j >= 0; --j) {
        dp[j + c] = max(dp[j + c], dp[j] + v);
      }
      cnt += c;  // 记录当前可能的最大核心数
    } else {     // 卖出
      for (int j = 0; j <= cnt - c; ++j) {
        dp[j] = max(dp[j], dp[j + c] + v);
      }
    }
  }
  for (int i = 0; i <= cnt; ++i) ans = max(ans, dp[i]);
  cout << ans << endl;
}
